package com.foreknow.demo10;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * 预处理PreparedStatement接口：可以执行动态的SQL语句
 * PreparedStatement pstmt = con.prepareStatement("UPDATE EMPLOYEES
 *                                      SET SALARY = ? WHERE ID = ?");
 *    pstmt.setBigDecimal(1, 153833.00)
 *    pstmt.setInt(2, 110592)
 */
public class PreparedStatementDemo {
    public static void main(String[] args) {
        try {
            //1. 加载驱动程序
            Class.forName("com.mysql.jdbc.Driver");
            //2. 获取连接(Connection)
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/basic2","root","123456");
            //3. 获取预处理对象
            PreparedStatement pstmt = conn.prepareStatement("insert into admin(username,password) values (?,?)");
            //将？替换为具体的值
            pstmt.setString(1,"xxx");
            pstmt.setString(2,"123456xxx");
            //执行添加操作
            int isRigth = pstmt.executeUpdate();
            System.out.println(isRigth);
            //关闭资源
            pstmt.close();
            conn.close();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }
}
